import { _decorator, Component, Node,Label,Sprite,SpriteFrame,assetManager,ImageAsset,Texture2D,Color,color} from 'cc';
import {RankUI} from "./RankUI";
import {GameMain} from "./GameMain";
const { ccclass, property } = _decorator;

@ccclass('RankItem')
export class RankItem extends Component {

    public rankNumberLabel:Label=null;
    public headerSprite:Sprite=null;
    public nameLabel:Label=null;
    public bgSprite:Sprite=null;

    public levelLabel:Label=null;

    private inited:boolean=false;

    public static readonly ItemHeight:number=84;
    public static spCache={};

    public static normalColor:Color=color(255, 255, 255, 255);
    public static inRankColor:Color=color(112, 255, 118, 255);

    onLoad()
    {
        this.init();
    }

    public init()
    {
        this.rankNumberLabel=this.node.getChildByName("rankNumber").getComponent(Label);
        this.headerSprite=this.node.getChildByName("header").getComponent(Sprite);
        this.nameLabel=this.node.getChildByName("name").getComponent(Label);
        this.bgSprite=this.node.getChildByName("bg").getComponent(Sprite);

        this.levelLabel=this.node.getChildByName("levelLabel").getComponent(Label);

        this.inited=true;
    }


    public setData(idx:number, data)
    {
        if(!this.inited)
            this.init();

        let cityMode:boolean=true;
        if(!data.city)
            cityMode=false;
        if(cityMode)
        {
            this.headerSprite.node.active=false;

            this.rankNumberLabel.string=""+(idx+1);
            this.nameLabel.string=data.city;
            this.levelLabel.string=""+data.star+" 星星";
        }
        else
        {
            this.rankNumberLabel.string=""+(idx+1);
            this.nameLabel.string=data.username;

            this.levelLabel.string="玩到第 "+(data.levelId+1)+"关";

            if(data.avatarUrl && data.avatarUrl.length>0)
            {
                assetManager.loadRemote(data.avatarUrl, {ext:'.png'},(err, imageAsset)=>{
                    if(err)
                    {
                        console.log("*** err="+JSON.stringify(err));
                    }
                    else
                    {
                        let spFrame=RankItem.spCache[imageAsset._uuid];
                        if(!spFrame)
                        {
                            spFrame=new SpriteFrame();
                            let tex=new Texture2D();
                            tex.image=imageAsset;
                            spFrame.texture=tex;

                            RankItem.spCache[imageAsset._uuid]=spFrame;
                        }
                        this.headerSprite.spriteFrame=spFrame;
                    }
                });
            }
            else
            {
                this.headerSprite.spriteFrame=GameMain.instance.rankUI.defaultHeader;
            }
        }
    }

    public setInRank(inRank:boolean)
    {
        if(inRank)
            this.bgSprite.color=RankItem.inRankColor;
        else
            this.bgSprite.color=RankItem.normalColor;
    }

    start() {

    }

    update(deltaTime: number) {
        
    }
}

